package com.stylefeng.guns.modular.item.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.item.util.Const;
import com.stylefeng.guns.modular.item.util.ItemWarpper;
import com.stylefeng.guns.modular.item.util.getSubStrUtils;
import com.stylefeng.guns.modular.quickImg.model.Quickimg;
import com.stylefeng.guns.modular.quickImg.service.IQuickimgService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.item.model.Item;
import com.stylefeng.guns.modular.item.service.IItemService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-09-26 19:54:25
 */
@Controller
@RequestMapping("/item")
public class ItemController extends BaseController {

    private String PREFIX = "/item/item/";

    @Autowired
    private IItemService itemService;
    @Autowired
    private IQuickimgService quickimgService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "item.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/item_add")
    public String itemAdd(Model model) {
        List list = Const.getModelList();
        model.addAttribute("list",list);
        return PREFIX + "item_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/item_update/{itemId}")
    public String itemUpdate(@PathVariable Integer itemId, Model model) {
        List list = Const.getModelList();
        model.addAttribute("list",list);
        Item item = itemService.selectById(itemId);
        model.addAttribute("item",item);
        LogObjectHolder.me().set(item);
        return PREFIX + "item_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Item> list =  itemService.selectList(null);
        List<Map<String, Object>> list1  = BeanKit.listToMapList(list);
        return new ItemWarpper(list1).warp();
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Item item) {
        /*添加时间*/
        Date date = new Date();
        item.setDate(date);
        /*shiro中获得用户名*/
        ShiroUser user = (ShiroUser)SecurityUtils.getSubject().getPrincipal();
        item.setAuthor(user.account);
        itemService.insert(item);
        int id = item.getId();
        System.out.println("输出的主键是："+id);

        //添加中的图片信息,(图片路径)
        String content = item.getContent();
        /**取出第一张图片的信息**/
        List firstImgs = getSubStrUtils.getImgStr(content);
        String firstImg = null;
        if (firstImgs.size()!=0){
            firstImg = (String)firstImgs.get(0);
            Quickimg quickimg = new Quickimg();
            quickimg.setCategory(item.getCategory());
            quickimg.setNewsId(item.getId());
            quickimg.setUrl(firstImg);
            quickimg.setDate(date);
            quickimgService.insert(quickimg);
        }
        System.out.println("第一张图片的位置："+firstImg);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer itemId) {
        /*根据id，查出时间并且根据时间将快速通道入口的图片删除*/
        Item item = itemService.selectById(itemId);
        /*添加条件判断,防止为空指针*/
        if(item!=null){
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("newsId",item.getId());
            quickimgService.delete(wrapper);
        }
        itemService.deleteById(itemId);

        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Item item) {
        itemService.updateById(item);
        return SUCCESS_TIP;
    }
    /***
     * 点击查看详情
     * **/
    @RequestMapping(value = "/detailYang/{itemId}")
    public String detailYang(@PathVariable("itemId") Integer itemId,Model model){
        Item item = itemService.selectById(itemId);
        model.addAttribute("item",item);
        return PREFIX + "item_detail.html";
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{itemId}")
    @ResponseBody
    public Object detail(@PathVariable("itemId") Integer itemId) {
        return itemService.selectById(itemId);
    }
}
